/*
 * BottomToolbar.java
 *
 * Created on 15 Haziran 2008 Pazar, 02:15
 */

package com.pegaa.uploader.ui.fileselection;

import com.pegaa.uploader.common.TargetFolderData;
import com.pegaa.uploader.config.ConfigHolder;
import com.pegaa.uploader.lang.Lang;
import com.pegaa.uploader.tools.CustomLog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;


/**
 *
 * @author  tayfun
 */
public class BottomToolbar extends javax.swing.JPanel {
    
    private ConfigHolder configHolder = null;
    private ArrayList<ActionListener> listeners = null;
    private DefaultComboBoxModel comboModel = null;
            
    /** Creates new form BottomToolbar */
    public BottomToolbar() {
        initComponents();
        this.listeners = new ArrayList<java.awt.event.ActionListener>(2);
    }
    
    public void setConfigHolder(ConfigHolder configHolder)
    {
        this.configHolder = configHolder;
        initTargetFolders();
        updateStrings();
    }

    /**
     * Show target folders select box.
     * 
     */
    private void initTargetFolders()
    {
        comboModel = (DefaultComboBoxModel) this.targetFolderList.getModel();
        comboModel.addElement(new TargetFolderData("--------::-------"));
        
        ArrayList<TargetFolderData> targetFolders = (ArrayList)this.configHolder.getObject("global.idMap");
        if(targetFolders == null){
            this.targetFolderList.setVisible(false);
            return;
        }
        int len = targetFolders.size();
        if(len > 0)
        {
            int selected = -1;
            String selectedTargetFolderID = ((String)this.configHolder.getObject("global.selectedFolder"));
            
            CustomLog.log("initing BottomToolbar selectedTargetFolderID = " + selectedTargetFolderID);
            
            for(int i=0; i<len; i++)
            {
                TargetFolderData currentData = (TargetFolderData) targetFolders.get(i);
                if(selectedTargetFolderID != null){
                    if(selectedTargetFolderID.equals(currentData.getTargetFolderID())){
                        selected = i;
                    }
                }
                comboModel.addElement(currentData);
            }
            CustomLog.log("BottomToolbar.initTargetFolders().selected = " + selected);
            if(selected > -1){
                this.targetFolderList.setSelectedIndex(selected+1); //0 is empty index
            }
        }else{
            this.targetFolderList.setVisible(false);
        }
    }
    
    /*
     *  Update component's components' text to the selected language
     * 
     */
    private void updateStrings()
    {
        Lang lang = (Lang)this.configHolder.getObject("global.lang");
        //this.buttonHelp.setText(lang.get("bottomtoolbar.help"));
        this.buttonUpload.setText(lang.get("bottomtoolbar.upload"));
        
        TargetFolderData data = (TargetFolderData)comboModel.getElementAt(0);
        data.setText(lang.get("bottomtoolbar.choose"));
    }
    
    public void addActionListener(java.awt.event.ActionListener l)
    {
        this.listeners.add(l);
    }
    
    /**
     * Notifies listeners for actions. In this component the event is upload
     * button clicked event.
     * 
     * 
     * @param e
     */
    private void notifyListeners(ActionEvent e)
    {
        int len = this.listeners.size();
        for(int i=0; i<len; i++)
        {
            this.listeners.get(i).actionPerformed(e);
        }
    }
    
    /**
     *  Controls selected targets and if one is selected notifies 
     * parent to take action.
     * Sends the selected target folder as source object
     * 
     */
    private void controlSelectionAndStartUpload()
    {
        TargetFolderData selectedFolder = null;
        if(this.targetFolderList.isVisible()){
            selectedFolder = (TargetFolderData)this.targetFolderList.getSelectedItem();
        }
        if(selectedFolder == null){
            selectedFolder = new TargetFolderData("::");
        }
        this.notifyListeners(new ActionEvent(selectedFolder.getTargetFolderID(), 0, "upload-button"));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        buttonUpload = new javax.swing.JButton();
        targetFolderList = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();

        setPreferredSize(new java.awt.Dimension(120, 100));
        setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT, 10, 10));

        buttonUpload.setText("Upload");
        buttonUpload.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                buttonUploadMouseClicked(evt);
            }
        });
        jPanel2.add(buttonUpload);

        jPanel2.add(targetFolderList);

        add(jPanel2, java.awt.BorderLayout.CENTER);

        jPanel1.setPreferredSize(new java.awt.Dimension(150, 100));
        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER, 5, 10));
        add(jPanel1, java.awt.BorderLayout.WEST);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonUploadMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_buttonUploadMouseClicked
        this.controlSelectionAndStartUpload();
}//GEN-LAST:event_buttonUploadMouseClicked
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonUpload;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JComboBox targetFolderList;
    // End of variables declaration//GEN-END:variables
    
}
